// 登录页面 第一次一键登录 需要弹窗填写用户名
const account = require('../../js/modules/account.js')
const componentCallback = require('../../js/common/componentCallback')
import authorize from '../../js/modules/authorize.js'
import { profile } from '../../js/modules/store.js'
import { alert, isEmpty } from '../../js/newUtil.js'
import {
  storage
} from '../../js/util'

Component({
  behaviors: [componentCallback],
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showComponent: false,
    title: '请填写名称', // 标题
    itemName: '客户名称', // 名称
    placeholder: '请输入客户名称', // 提示文字
    name: '', // 输入内容（用户名）
    required: true, // 是否必须填写
    needShow: true, // 是否需要显示，为false时不显示弹窗
    forms: { // 用于提交的表单数据信息，等同于设置里的修改用户信息页面
      Name: {
        value: "",
        error: "请输入名称"
      },
    },
    profileToken: '', // 登录未成功时返回的临时token（原用于提交用户自定义信息）
    userId: '', // 登录成功时获取的用户id
  },

  lifetimes: {
    attached() {
      this.getCustomize()
      this.refreshData()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showComponent (options) {
      console.log(options)
      if (options.profileToken) {
        this.setData({
          profileToken: options.profileToken
        })
      }
      if (options.userId) {
        this.setData({
          userId: options.userId
        })
      }
      this.refreshData()
    },
    refreshData() {
      let forms = this.data.forms || ''
      forms.Name.value = ''
      this.setData({
        name: '',
        forms: forms
      })
    },
    cancel () {
      this.setData({
        showComponent: false
      })
      let fail = this.data.componentFail
      fail()
    },
    getCustomize:function(){
      let that = this;
      account.register.getCustomize(function(res){
        if (res.Success) {
          that.setData({
            placeholder: res.Content.Placeholder || '',
            itemName: res.Content.Name || '',
            // required: res.Content.Required == 1 ? true : false,
            // needShow: res.Content.Status == 1 ? true : false
          })
        }
      })
    },
    input (e) {
      let name = e.currentTarget.dataset.name
      let value = e.detail.value
      this.setData({
        [name]: value
      })
    },
    checkData () {
      let {
        name,
        placeholder,
        forms
      } = this.data
      console.log(this.data)
      if (isEmpty(name)) {
        alert(placeholder || '请输入名称')
        return
      }
      forms.Name.value = name
      this.setData({
        forms
      })
      this.submit(forms)
    },
    submit (forms) {
      let {
        profileToken,
        userId,
        name,
        componentSuccess
      } = this.data
      profile.editUserName(name, profileToken, userId).then((res) => {
        let entity = authorize.getEntity() || {}
        let role = entity.role || {}
        // 将全局的名称改为修改后的名字
        if (!isEmpty(entity) && !isEmpty(role)) {
          role.Name = name
          entity.role = role
          storage.put('LoginSessionKey', entity, 0)
        }
        componentSuccess()
      }).catch((err) => {
        alert(err.Message || '修改失败')
        console.log(err)
      })
    }
  }
})